//
//  JSTPersonalHeaderImageView.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/25.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTPersonalHeaderImageView.h"

@interface JSTPersonalHeaderImageView ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *userBtn;

@property (nonatomic, strong) UIButton *loginBtn;

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UILabel *userNickNameLabel;

@end

@implementation JSTPersonalHeaderImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleLabel = [[UILabel alloc] init];
        [self addSubview:_titleLabel];
        
        _iconImageView = [[UIImageView alloc] init];
        [self addSubview:_iconImageView];
        
        _userNickNameLabel = [[UILabel alloc] init];
        [self addSubview:_userNickNameLabel];
    }
    return self;
}

- (void) createContent:(JSTUserPublicModel *)model
{
    _titleLabel.frame = CGRectMake(0, 20, kDeviceWidth, 44);
    _titleLabel.text = @"个人中心";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    CGFloat iconX = (kDeviceWidth - kCurrentScreenWidth(60))/2.;
    CGFloat iconY = CGRectGetMaxY(_titleLabel.frame) + kCurrentScreenWidth(20);
    CGFloat iconW = kCurrentScreenWidth(60);
    CGFloat iconH = iconW;
    _iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    _iconImageView.layer.masksToBounds = YES;
    _iconImageView.layer.cornerRadius = iconW/2.;
    [_iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    
    CGFloat nickX = 0;
    CGFloat nickY = CGRectGetMaxY(_iconImageView.frame) + 5;
    CGFloat nickW = kDeviceWidth;
    CGFloat nickH = kCurrentScreenWidth(30);
    _userNickNameLabel.frame = CGRectMake(nickX, nickY, nickW, nickH);
    _userNickNameLabel.text = model.nickName;
    _userNickNameLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _userNickNameLabel.textAlignment = NSTextAlignmentCenter;
    
    
//    if (!IsStrEmpty(model.token))
//    {
//        [self.loginBtn removeFromSuperview];
//        
//        CGFloat userX = (kDeviceWidth - kCurrentScreenWidth(60))/2.;
//        CGFloat userY = CGRectGetMaxY(_titleLabel.frame) + kCurrentScreenWidth(20);
//        CGFloat userW = kCurrentScreenWidth(60);
//        CGFloat userH = kCurrentScreenWidth(100);
//        self.userBtn.frame = CGRectMake(userX, userY, userW, userH);
//        [self.userBtn addTarget:self action:@selector(changeUserController) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:self.userBtn];
//        
//        UIImageView *iconImageView = [[UIImageView alloc] init];
//        iconImageView.frame = CGRectMake(0, 0, kCurrentScreenWidth(60), kCurrentScreenWidth(60));
//        [iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
//        iconImageView.layer.masksToBounds = YES;
//        iconImageView.layer.cornerRadius = self.userBtn.width/2.;
//        [self.userBtn addSubview:iconImageView];
//        
//        UILabel *nickLabel = [[UILabel alloc] init];
//        nickLabel.frame = CGRectMake(0, CGRectGetMaxY(iconImageView.frame) + kCurrentScreenWidth(5), self.userBtn.width, kCurrentScreenWidth(30));
//        nickLabel.text = model.nickName;
//        nickLabel.textAlignment = NSTextAlignmentCenter;
//        nickLabel.font = kSystemFont(kCurrentScreenWidth(10));
//        [self.userBtn addSubview:nickLabel];
//    }else
//    {
//        [self.userBtn removeFromSuperview];
//        
//        CGFloat loginX = (kDeviceWidth - kCurrentScreenWidth(60))/2.;
//        CGFloat loginY = CGRectGetMaxY(_titleLabel.frame) + kCurrentScreenWidth(50);
//        CGFloat loginW = kCurrentScreenWidth(60);
//        CGFloat loginH = kCurrentScreenWidth(100);
//        self.loginBtn.frame = CGRectMake(loginX, loginY, loginW, loginH);
//        [self.loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
//        [self.loginBtn addTarget:self action:@selector(changeLoginController) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:self.loginBtn];
//    }
}

- (void) changeLoginController
{
    if (_delegate && [_delegate respondsToSelector:@selector(changeLoginController)]) {
        [_delegate changeLoginController];
    }
}

- (void) changeUserController
{
    if (_delegate && [_delegate respondsToSelector:@selector(changeUserController)]) {
        [_delegate changeUesrController];
    }
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc] init];
    }
    return _loginBtn;
}

- (UIButton *)userBtn
{
    if (!_userBtn) {
        _userBtn = [[UIButton alloc] init];
    }
    return _userBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
